Skip to content

[clang-format] Fix mismatched break in BlockIndent #124998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2025

Conversation

gedare
Copy link
Contributor

@gedare gedare commented Jan 29, 2025

Near the ColumnLimit a break could be inserted before a right parens with BlockIndent without a break after the matching left parens. Avoid these hanging right parens by disallowing breaks before right parens unless there was a break after the left parens.

Fixes #103306

Near the ColumnLimit a break could be inserted before a right parens with
BlockIndent without a break after the matching left parens. Avoid these
hanging right parens by disallowing breaks before right parens unless
there was a break after the left parens.

Fixes llvm#103306
@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2025

@llvm/pr-subscribers-clang-format

Author: Gedare Bloom (gedare)

Changes

Near the ColumnLimit a break could be inserted before a right parens with BlockIndent without a break after the matching left parens. Avoid these hanging right parens by disallowing breaks before right parens unless there was a break after the left parens.

Fixes #103306


Full diff: https://github.com/llvm/llvm-project/pull/124998.diff

2 Files Affected:

  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+7)
  • (modified) clang/unittests/Format/FormatTest.cpp (+4)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index c311deaa17bb0e..d0a09d305baaf7 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -349,6 +349,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
     }
   }
 
+  // Allow breaking before the right parens with block indentation if there was
+  // a break after the left parens, which is tracked by BreakBeforeClosingParen
+  if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+      Current.is(tok::r_paren)) {
+    return CurrentState.BreakBeforeClosingParen;
+  }
+
   // Don't allow breaking before a closing brace of a block-indented braced list
   // initializer if there isn't already a break.
   if (Current.is(tok::r_brace) && Current.MatchingParen &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 57f12221cdc7e6..e647c4fff78b0e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9608,6 +9608,10 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
       "    \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
       ");",
       Style);
+  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaa(\n"
+               "    &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+               ");",
+               Style);
   Style.ColumnLimit = 60;
   verifyFormat("auto lambda =\n"
                "    [&b](\n"

@owenca owenca merged commit b873479 into llvm:main Jan 31, 2025
6 of 7 checks passed
@gedare gedare deleted the eol-blockindent-bug branch January 31, 2025 04:56
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 25, 2025
Near the ColumnLimit a break could be inserted before a right parens
with BlockIndent without a break after the matching left parens. Avoid
these hanging right parens by disallowing breaks before right parens
unless there was a break after the left parens.

Fixes llvm#103306

(cherry picked from commit b873479)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] Weird line breaks with AlignAfterOpenBracket: BlockIndent
3 participants